package com.common.base.widget.tab;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.TextView;

public class RectScaleAlphaTabAnimation implements TabAnimation{

    @Override
    public void onDraw(XTabLayout view, Canvas canvas, BitmapDrawable drawable, int centerX, int centerY, int width, int height, float offset) { }

    @Override
    public void onDraw(XTabLayout view, Canvas canvas, IndicatorScrollData data) {

        if(data.isNoScroll && data.newTagView != null) {
            drawRoundRect(view, canvas, getBound(view, data.newTagView), 1F);
            setTextColor(view, data.tvNew, true);
            setTextColor(view, data.tvOld, false);
            return ;
        }

        if((data.mCurrentPositionOffset <= 0F)&& data.oldTagView != null) {
            drawRoundRect(view, canvas, getBound(view, data.oldTagView), 1F);
            setTextColor(view, data.tvOld, true);
            setTextColor(view, data.tvNew, false);
            return ;
        }

        if(data.oldTagView != null)
            drawRoundRect(view, canvas, getBound(view, data.oldTagView, 1F - data.mCurrentPositionOffset), 1F - data.mCurrentPositionOffset);
        if(data.newTagView != null)
            drawRoundRect(view, canvas, getBound(view, data.newTagView, data.mCurrentPositionOffset), data.mCurrentPositionOffset);
    }

    private void setTextColor(XTabLayout view, TextView tv, boolean isSelected) {
        if(tv != null) {
            tv.setTextColor(isSelected ? view.getTextSelectColor() : view.getTextUnselectColor());
        }
    }

    private void drawRoundRect(XTabLayout view, Canvas canvas, Rect bound, float percent) {
        Drawable drawable = view.getContext().getResources().getDrawable(view.getIndicatorDrawableSrc());
        drawable.setBounds(bound);
        drawable.setAlpha(getAlpha(percent));
        drawable.draw(canvas);
    }

    //颜色透明度的百分比
    private int getAlpha(float percent) {
        return (int)(255F * percent);
    }

    private Rect getBound(XTabLayout tab, View view) {
        return new Rect(
                (int)(view.getLeft() + tab.getIndicatorMarginLeft())
                , (int)(view.getTop() + tab.getIndicatorMarginTop())
                , (int)(view.getRight() - tab.getIndicatorMarginRight())
                , (int)(view.getBottom() - tab.getIndicatorMarginBottom()));
    }

    private Rect getBound(XTabLayout tab, View view, float percent) {
        int left = (int)(view.getLeft() + tab.getIndicatorMarginLeft());
        int top = (int)(view.getTop() + tab.getIndicatorMarginTop());
        int right = (int)(view.getRight() - tab.getIndicatorMarginRight());
        int bom = (int)(view.getBottom() - tab.getIndicatorMarginBottom());
        int w = (int)((float)(right - left) * percent);
        int h = (int)((float)(bom - top) * percent);
        return new Rect( right - w, bom - h, right, bom);
    }
}
